package com.lsh.controller;

import com.github.pagehelper.PageInfo;
import com.lsh.domain.UserComment;
import com.lsh.domain.UserReply;
import com.lsh.service.CommentService;
import com.lsh.service.IndexService;
import com.lsh.service.PraiseService;
import com.lsh.util.result.Result;
import com.sun.org.apache.bcel.internal.generic.FADD;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 文章评价/回复模块
 */
@RestController
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    PraiseService praiseService;

    @Autowired
    IndexService indexService;

    /**
     * 文章评价列表
     * @param artId
     * @return
     */
    @PostMapping(value = "/article/comment/list", produces = {"application/json;charset=UTF-8"})
    public String showCommentList(@RequestParam("art_id") String artId, @CookieValue(value = "TOKEN", required = false) String token){
        List<HashMap<String, Object>> commentList = commentService.getCommentList(artId, token);
        JSONObject jo = new JSONObject();
        jo.put("data", commentList);
        return jo.toString();
    }

    /**
     * 文章发表评价
     * @return
     */
    @PostMapping("/user/article/comment/publish")
    public String publishComment(@CookieValue("TOKEN") String token, UserComment userComment){
        String result = commentService.publishComment(token, userComment);
        JSONObject jo = new JSONObject();
        jo.put("code", result);
        return jo.toString();
    }

    /**
     * 文章回复列表
     * @param commentId
     * @return
     */
    @PostMapping(value = "/article/reply/list", produces = {"application/json;charset=UTF-8"})
    public String showReplyList(@RequestParam("comment_id") String commentId,  @CookieValue(value = "TOKEN", required = false) String token){
        List<HashMap<String, Object>> replyList = commentService.getReplyList(commentId, token);
        JSONObject jo = new JSONObject();
        jo.put("data", replyList);
        return jo.toString();
    }

    /**
     * 用户回复
     * @param token
     * @param userReply
     * @return
     */
    @PostMapping("/user/article/reply/publish")
    public String publishReply(@CookieValue("TOKEN") String token, UserReply userReply){
        String result = commentService.publishReply(token, userReply);
        JSONObject jo = new JSONObject();
        jo.put("code", result);
        return jo.toString();
    }

    /**
     * 删除指定评价
     * @param token
     * @param commentId
     * @return
     */
    @PostMapping("/user/article/comment/delete")
    public String deleteComment(@CookieValue("TOKEN") String token, @RequestParam("comment_id") String commentId){
        String result = commentService.deleteComment(token, commentId);
        JSONObject jo = new JSONObject();
        jo.put("code", result);
        return jo.toString();
    }

    /**
     * 删除指定回复
     * @param token
     * @param replyId
     * @return
     */
    @PostMapping("/user/article/reply/delete")
    public String deleteReply(@CookieValue("TOKEN") String token, @RequestParam("reply_id") String replyId){
        String result = commentService.deleteReply(token, replyId);
        JSONObject jo = new JSONObject();
        jo.put("code", result);
        return jo.toString();
    }

    /**
     * 点赞评价
     * @param token
     * @return
     */
    @PostMapping("/user/article/comment/praise")
    public String praiseComment(@CookieValue("TOKEN") String token, @RequestParam("comment_id") String commentId){
        String result = praiseService.praiseComment(token, commentId);
    return result;
    }

    /**
     * 点赞回复
     * @param token
     * @param replyId
     * @return
     */
    @PostMapping("/user/article/reply/praise")
    public String praiseReply(@CookieValue("TOKEN") String token, @RequestParam("reply_id") String replyId){
        String result = commentService.praiseReply(token, replyId);
        return result;
    }
}
